# coding = utf-8

'''
将chengkung的数据格式转换成origion的数据格式
'''

import os
import numpy as np
from pathlib2 import Path


def converse_data_to_origion():
    destination = "/datasets/3Dircadb/chengkung"
    origion_path = Path("/datasets/3Dircadb/origion")

    for i in range(20):
        destination_path = os.path.join(destination, "case_{}".format(str(i).zfill(5)))
        destination_image_path = os.path.join(destination_path, "imaging")
        destination_mask_path = os.path.join(destination_path, "segmentation")
        assert len(os.listdir(destination_image_path)) == len(os.listdir(destination_mask_path)), destination_path
        length = len(os.listdir(destination_image_path))
        image_list = []
        mask_list = []
        for j in range(length):
            image_file = os.path.join(destination_image_path, "{}.npy".format(str(j).zfill(3)))
            mask_file = os.path.join(destination_mask_path, "{}.npy".format(str(j).zfill(3)))
            image = np.load(image_file)
            mask = np.load(mask_file)
            image_list.append(image.reshape(1,image.shape[0], image.shape[1]))
            mask_list.append(mask.reshape(1,mask.shape[0], mask.shape[1]))

        image_list = np.concatenate(image_list, axis=0)
        mask_list = np.concatenate(mask_list, axis=0)
        print(i,image_list.shape, mask_list.shape)

        origion_case_path = origion_path / "case_{}".format(str(i).zfill(5))
        if not origion_case_path.exists():
            origion_case_path.mkdir(parents=True)

        origion_case_path_image = origion_case_path/"image.npy"
        origion_case_path_mask = origion_case_path/"segment.npy"

        np.save(origion_case_path_image, image_list)
        np.save(origion_case_path_mask, mask_list)




if __name__ == '__main__':
    converse_data_to_origion()